<?php

namespace app\data\service;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use think\admin\Service;
use AlibabaCloud\SDK\Dyvmsapi\V20170525\Dyvmsapi;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use AlibabaCloud\SDK\Dyvmsapi\V20170525\Models\SingleCallByTtsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use think\facade\Log;

/**
 * 阿里云短信支持服务
 * Class AliMessageService
 * @package app\data\service
 */
class AliSingleCallService extends Service
{
    /**
     * 平台授权账号
     * @var string
     */
    protected $username;

    /**
     * 平台授权密码
     * @var string
     */
    protected $password;

    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dyvmsapi Client
     */
    public static function createClient(){
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => env('ACCESS_KEY_ID', ''),
            // 您的AccessKey Secret
            "accessKeySecret" => env('ACCESS_KEY_SECRET', '')
        ]);
        // 访问的域名
        $config->endpoint = "dyvmsapi.aliyuncs.com";
        return new Dyvmsapi($config);
    }

    /**
     * 语音通知
     * @param $calledNumber
     * @param $ttsCode
     * @param $ttsParam
     * @return void
     */
    public static function singleCallByTts($calledNumber, $ttsCode, $ttsParam)
    {
        $client = self::createClient();
        $params = [
            "calledNumber" => $calledNumber,
            "ttsCode" => $ttsCode
        ];
        if (!empty($ttsParam)) {
            $params['ttsParam'] = json_encode($ttsParam);
        }

        $singleCallByTtsRequest = new SingleCallByTtsRequest($params);
        $runtime = new RuntimeOptions([]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $client->singleCallByTtsWithOptions($singleCallByTtsRequest, $runtime);
        }
        catch (Exception $error) {
            Log::error('语音通知消息异常' . $error->getMessage());
        }
    }
}